#include "UIWidget.h"

UIWidget::UIWidget():
m_bEnable(true),
m_bMoveable(false)
{
	m_bIgnoreAnchorPointForPosition = false;
}

UIWidget::~UIWidget()
{

}

bool UIWidget::init()
{
	bool bRet = CCLayer::init();

	setTouchEnabled(true);

	return bRet;
}

UIWidget* UIWidget::create()
{
	UIWidget *pRet = new UIWidget();
	if (pRet && pRet->init())
	{
		pRet->autorelease();
		return pRet;
	}
	else
	{
		CC_SAFE_DELETE(pRet);
		return NULL;
	}
}

CCRect UIWidget::rect()
{
	return CCRectMake( m_tPosition.x - m_tContentSize.width * m_tAnchorPoint.x, 
		m_tPosition.y - m_tContentSize.height * m_tAnchorPoint.y,
		m_tContentSize.width, m_tContentSize.height);
}

void UIWidget::registerWithTouchDispatcher()
{
	CCDirector* pDirector = CCDirector::sharedDirector();
	pDirector->getTouchDispatcher()->addTargetedDelegate(this, 0, true);
}

bool UIWidget::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent)
{
	return true;
}

void UIWidget::ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent)
{
	if (m_bMoveable)
	{
		CCDirector* pDirector = CCDirector::sharedDirector();
		CCPoint curPos = pDirector->convertToGL(pTouch->locationInView());
		CCPoint lastPos = pDirector->convertToGL(pTouch->previousLocationInView());
		setPosition(ccpAdd(curPos, ccpSub(curPos, lastPos)));
	}
}

void UIWidget::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent)
{

}